<template>
	<view class="uni-navbar">
		<view :class="{ 'uni-navbar--fixed': fixed, 'uni-navbar--shadow': shadow, 'uni-navbar--border': border }" class="uni-navbar__content">
			<my-status-bar v-if="statusBar" :backgroundColor="absolute?'#ffffff':backgroundColor" />
			<view :style="'background-color:' + backgroundColor + ';height:' + height + 'rpx;line-height:' + height + 'rpx;'" class="uni-navbar__header uni-navbar__content_view">
				<view @tap="onClickLeft" class="uni-navbar__header-btns uni-navbar__header-btns-left uni-navbar__content_view">
					<!-- #ifndef MP-ALIPAY -->
					<view class="uni-navbar__content_view" v-if="leftIcon">
						<view class="sideImg left" :style="leftImgStyle"></view>
					</view>
					<view class="uni-navbar-btn-text uni-navbar__content_view" v-if="leftText.length && !leftIcon" :style="{ color: leftColor, fontSize: leftFontSize + 'rpx' }">
						{{ leftText }}
					</view>
					<slot name="left" />
					<!-- #endif -->
				</view>
				<view class="uni-navbar__header-container uni-navbar__content_view">
					<view class="uni-navbar__header-container-inner uni-navbar__content_view" v-if="title.length" :style="{ color: titleColor, fontSize: titleFontSize + 'rpx' }">
						{{ title }}
					</view>
					<!-- 标题插槽 -->
					<slot />
				</view>
				<view @tap="onClickRight" class="uni-navbar__header-btns uni-navbar__header-btns-right uni-navbar__content_view">
					<!-- 优先显示图标 -->
					<view class="uni-navbar-btn-text uni-navbar__content_view" v-if="rightText.length && !rightIcon" :style="{ color: rightColor, fontSize: rightFontSize + 'rpx' }">
						{{ rightText }}
					</view>
					<view class="uni-navbar__content_view" v-if="rightIcon">
						<view class="sideImg" :style="rightImgStyle"></view>
					</view>
					<slot name="right" />
				</view>
			</view>
		</view>
		<view class="uni-navbar__placeholder" v-if="fixed">
			<my-status-bar v-if="statusBar" />
			<view class="uni-navbar__placeholder-view" :style="{ height: height + 'rpx' }" v-if="!absolute" />
		</view>
	</view>
</template>

<script>
import MyStatusBar from '../my-status-bar/my-status-bar.vue'
export default {
	name: 'MyNavBar',
	components:{MyStatusBar},
	props: {
		title: {
			type: String,
			default: ''
		},
		leftText: {
			type: String,
			default: ''
		},
		rightText: {
			type: String,
			default: ''
		},
		leftIcon: {
			type: [Boolean, String],
			default: false
		},
		rightIcon: {
			type: [Boolean, String],
			default: false
		},
		fixed: {
			type: [Boolean, String],
			default: true
		},
		absolute: {
			type: [Boolean, String],
			default: false
		},
		titleColor: {
			type: String,
			default: '#000000'
		},
		leftColor: {
			type: String,
			default: '#000000'
		},
		rightColor: {
			type: String,
			default: '#000000'
		},
		backgroundColor: {
			type: String,
			default: 'transparent'
		},
		statusBar: {
			type: [Boolean, String],
			default: true
		},
		shadow: {
			type: [String, Boolean],
			default: false
		},
		border: {
			type: [String, Boolean],
			default: false
		},
		leftFontSize: {
			type: Number,
			default: 28
		},
		rightFontSize: {
			type: Number,
			default: 28
		},
		titleFontSize: {
			type: Number,
			default: 32
		},
		height: {
			type: Number,
			default: 88
		},
		leftImg: {
			type: String,
			default: ''
		},
		leftFileName: {
			type: String,
			default: ''
		},
		leftIconParam: {
			type: String,
			default: ''
		},
		rightImg: {
			type: String,
			default: ''
		},
		rightFileName: {
			type: String,
			default: ''
		},
		rightIconParam: {
			type: String,
			default: ''
		},
	},
	methods: {
		onClickLeft() {
			this.$emit('clickLeft');
		},
		onClickRight() {
			this.$emit('clickRight');
		}
	},
	computed: {
		ratio1() {
			return (width,iconFileParam)=>{
				var iconW = 1;
				var icon = this.$icons[iconFileParam]
				if(icon){
					iconW = icon.width
				}
				return width / iconW;
			}
		},
		ratio2() {
			return (height,iconFileParam)=>{
				var iconH = 1;
				var icon = this.$icons[iconFileParam]
				if(icon){
					iconH = icon.height
				}
				return height / iconH;
			}
		},
		left() {
			return (width,iconFileParam)=>{
				var iconX = 0;
				var icon = this.$icons[iconFileParam]
				if(icon){
					iconX = icon.x
				}
				return -iconX * this.ratio1(width,iconFileParam);
			}
		},
		top() {
			return (height,iconFileParam)=>{
				var iconY = 0;
				var icon = this.$icons[iconFileParam]
				if(icon){
					iconY = icon.y
				}
				return -iconY * this.ratio2(height,iconFileParam);
			}
		},
		iconStyle(){
			return (img,param,width,height)=>{
				var style = 'width:'+width+'rpx;height:'+height+'rpx;';
				if (img) {
					style += 'background-image:url(' + img + ');background-size:100% 100%;background-position:0 0;';
				} else {
					style +=
						'background-image:url(' + this.$skinUrl + ');background-size:' +
						this.$skinW * this.ratio1(width,param) +
						'rpx ' +
						this.$skinH * this.ratio2(height,param) +
						'rpx;' +
						'background-position:' +
						this.left(width,param) +
						'rpx ' +
						this.top(height,param) +
						'rpx;';
				}
				return style
			}
		},
		leftImgStyle() {
			return this.iconStyle(this.leftImg,this.leftIconParam,16,28)
		},
		rightImgStyle(){
			return this.iconStyle(this.rightImg,this.rightIconParam,48,48)
		}
	}
};
</script>

<style lang="scss" scoped>
$nav-height: 44px;
.uni-navbar-btn-text {
	font-size: 32rpx;
}

.uni-navbar {
	width: 750rpx;
	overflow: hidden;
}

.uni-navbar__content {
	position: relative;
	width: 750rpx;
	// background-color: $uni-bg-color;
	overflow: hidden;
}
.uni-navbar__content.boxNoRealtive{
	position: unset;
}

.uni-navbar__content_view {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	align-items: center;
	flex-direction: row;
	// background-color: #FFFFFF;
}
.sideImg{
	background-repeat: no-repeat;
}
.sideImg.left{
	margin: 0 14rpx;
}
.uni-navbar__header {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	flex-direction: row;
	width: 750rpx;
	height: $nav-height;
	line-height: $nav-height;
	font-size: 32rpx;
	// background-color: #ffffff;
}

.uni-navbar__header-btns {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	flex-wrap: nowrap;
	height: 100%;
	// min-width: 60rpx;
	padding: 0 16rpx;
	justify-content: center;
	align-items: center;
	&:active {
		background-color: rgba(0, 0, 0, 0.1);
	}
}

.uni-navbar__header-btns-left {
	justify-content: flex-start;
}

.uni-navbar__header-btns-right {
	justify-content: flex-end;
}

.uni-navbar__header-container {
	flex: 1;
	height: 100%;
}

.uni-navbar__header-container-inner {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	flex: 1;
	align-items: center;
	justify-content: center;
	font-size: 28rpx;
}

.uni-navbar__placeholder-view {
	height: $nav-height;
}

.uni-navbar--fixed {
	position: fixed;
	z-index: 998;
}

.uni-navbar--shadow {
	/* #ifndef APP-NVUE */
	box-shadow: 0 1px 6px #ccc;
	/* #endif */
}

.uni-navbar--border {
	border-bottom-width: 1rpx;
	border-bottom-style: solid;
	border-bottom-color: #c8c7cc;
}
</style>
